<?php
/*
 * 推送订单到海关
 * */
defined('IN_NWIDC') or exit('Access Invalid!');

class push_customsControl extends BaseSellerControl
{
  private $pushOrderIds = array();
  private $paymentConf = array();
  public function __construct()
  {
    parent::__construct();
  }

  // 需要提交的订单号
  public function ordersOp(){
    $payment = $_POST["payment"] ? $_POST["payment"] : "joinpay";
    $payment .= "_push";
    $this->paymentConf = array(
      "merchantNo" => "888105600009620", // 商家号
      "merchantKey" => "8b5893d6c2eb4e6faf0690b7a355fc02",  // 商家密钥
      "tmallCode" => '4403160CTP', // 电商平台代码
      "tmallName" => "深圳市明易科技有限公司",  // 电商平台名称
      'customsCode' => $_POST["custom"], // 海关代码
      'functionCode' => "CUS" // 申报类型
    );

    if($_POST["orders"] == "all"){
      // 选取符合条件的前五十条数据
      $orders = Model()->table("order")->field("order_sn")->where(array("payment_code"=>array("like", array("%joinpay%","wxpay", "OR")), "store_id"=>$this->store_info["store_id"],"order_state"=>ORDER_STATE_PAY))->limit(50)->select();
      foreach($orders as $val){
        array_push($this->pushOrderIds, $val["order_sn"]);
      }
    }else{
      $this->pushOrderIds = $_POST["orders"];
    }

    if(count($this->pushOrderIds)){
      // 调用选取的推送方式
      $this->$payment();
    }else{
      echo json_encode(array("无符合订单"));
    }
  }

  private function joinpay_sign($params){
    $merchantkey = $this->paymentConf["merchantKey"];
    ksort($params);
    $str = implode('',$params);
    $str .= $merchantkey;
    return md5($str);
  }

  private function joinpay_push(){
    $url = "https://www.joinpay.com/trade/pushedApi.action";
    $orderIds = $this->pushOrderIds;
   
    $results = [];

    foreach($orderIds as $val){
      $orderInfo = Model()->table("order")->where(array("order_sn"=>$val, "store_id"=>$this->store_info["store_id"], "is_tax_order"=>"1", "order_state"=>ORDER_STATE_PAY))->find();

      if( preg_match('/joinpay|wxpay/', $orderInfo["payment_code"]) ){
        $params = array(
          'pc01_MerchantNo' => $this->paymentConf["merchantNo"], // 商家号
          'pc02_OrderNo' => $val, // order_sn
          'pc03_CustomsCode' => $this->paymentConf["customsCode"], // 海关代码
          'pc04_FunctionCode' => $this->paymentConf["functionCode"], // 申报类型
          'pc06_DomainName' => WEB_DOMAIN, // 平台域名
          'pc07_TmallCode' => $this->paymentConf["tmallCode"], // 电商平台代码
          'pc08_TmallName' => $this->paymentConf["tmallName"], // 电商平台名称
          'pc09_PayerName' => $orderInfo["n_buyer_true_name"],// 姓名
          'pc10_PayerIdType' => '1', // 证件类型
          'pc11_PayerIdNo' => $orderInfo["n_buyer_idcard"], // 证件号
          'pc12_PayerTel' => $orderInfo["n_buyer_phone"] // 手机号
        );
        $params['hmac'] = $this->joinpay_sign($params);
        $res = $this->post_url($url, $params);

        if($res['code'] == 0){
          array_push($results, "{$val}，推送成功");
        }else{
          array_push($results, "{$val}，推送失败，错误代码为【{$res['code']}】,错误信息为 【{$res['message']}】");
        }

      }else{
        array_push($results, "{$val}，推送失败，非汇聚支付订单或未符合推送条件！");
      }
    }
    echo json_encode($results);
    return true;
  }

  private function post_url($url, $param, $decode = true){
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    if (!$decode) {
      return $response;
    }
    return json_decode($response,true);
  }

}